package com.weapons.week0903_weapons.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.adapter.RecommendListAdapter;
import com.weapons.week0903_weapons.bean.RecommentBean;
import com.weapons.week0903_weapons.constant.UrlString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author:DotaWang
 * time:2016/7/19 19:52
 */
public class RecommendFragment extends Fragment {
    private Activity mActivity;
    private String mpos = "";
    private RecyclerView mRvLists;
    private List<RecommentBean.ItemBean> item;
    private RecommendListAdapter adapter;

    public static RecommendFragment newInstance(String pos) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putString("pos", pos);
        fragment.setArguments(new Bundle(bundle));
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        item = new ArrayList<>();
        Bundle bundle = getArguments();
        if (bundle != null) {
            mpos = bundle.getString("pos");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mRvLists= (RecyclerView) view.findViewById(R.id.rv_lists);
        super.onViewCreated(view, savedInstanceState);
        getData();
        adapter = new RecommendListAdapter(mActivity, item);
        mRvLists.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(mActivity);
        mRvLists.setLayoutManager(manager);
    }

    private void getData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(UrlString.URL_RECOMMEND_CONTENT).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                RecommentBean recommentBean = new Gson().fromJson(result, RecommentBean.class);
                item = recommentBean.getItem();
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
